﻿@using Matrix.MaKaYunShang.BLL
@model Matrix.MaKaYunShang.Model.ShopSubsidiaryInfo
@{
    ViewBag.Title = "店铺充值";
    ViewBag.PathToBtn = "/Shops/List";
    ViewBag.TextBtn = "返回至列表";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                店铺充值
            </div>
            <div class="panel-body">
                <div class="row">
                    @using (Html.BeginForm("Charge", "Shops", FormMethod.Post, new { id = "frmUpdate", enctype = "multipart/form-data" }))
                    {
                        @Html.AntiForgeryToken()
                        @Html.ValidationSummary(true)

                        <div class="col-lg-12">
                            <fieldset>
                                <input type="hidden" id="Id" name="Id" value="@Model.Id"/>
                                @Html.HiddenFor(model => model.ShopId)
                                <div class="form-group col-md-6">
                                    <label>总充值金额</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.TotaChargeMoney, new { @class = "form-control", @readonly = "readonly" })
                                        <span class="input-group-addon red"></span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>店铺余额</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.AccountMoney, new { @class = "form-control", @readonly = "readonly" })
                                        <span class="input-group-addon red"></span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>平台佣金抽取总金额</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.PlatformTotalPayMoney, new { @class = "form-control", @readonly = "readonly" })
                                        <span class="input-group-addon red"></span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>商家佣金抽取总金额</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.ShopTotalPayMoney, new { @class = "form-control", @readonly = "readonly" })
                                        <span class="input-group-addon red"></span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>总发卡数量</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.TotalCanSendCard, new { @class = "form-control", @readonly = "readonly" })
                                        <span class="input-group-addon red"></span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>已发卡数量</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.AlreadySendCard, new { @class = "form-control", @readonly = "readonly" })
                                        <span class="input-group-addon red"></span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>本次充值金额（当前可发卡券数量与充值金额比例配置--@ViewBag.CardQuantityPerYuan : 1）</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.ChargeMoney, new { @class = "form-control", placeholder = "请填写本次充值金额", maxlength = 8 })
                                        <span class="input-group-addon red">必填</span>
                                    </div>
                                </div>
                                <div class="col-md-12">
                                    <button id="btnSave" type="button" class="btn btn-primary">保存</button>
                                </div>
                            </fieldset>
                        </div>
                    }
                </div>
                <div class="row" style="padding-left: 15px; padding-right: 15px; margin-top: 10px;">
                    <div class="col-lg-12">
                        <div class="panel panel-default">
                            <div class="panel-heading">
                                <h4>充值记录</h4>
                            </div>
                            <div class="panel-body">
                                <div class="table-responsive table-bordered">
                                    <table class="table">
                                        <thead>
                                            <tr>
                                                <th>充值金额</th>
                                                <th>充值前金额</th>
                                                <th>充值后金额</th>
                                                <th>充值时间</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @{
                                                var recordList = ShopMoneyChangeRecordBLL.Instance.GetModelList("ShopId=" + Model.ShopId + " and ChangeSource=0 and OperaResult=2").OrderByDescending(l=>l.AddTime);
                                                foreach (var item in recordList)
                                                {
                                                    <tr>
                                                        <td>@item.ChangeMoney</td>
                                                        <td>@item.BeforeMoney</td>
                                                        <td>@item.AfterMoney</td>
                                                        <td>@item.AddTime.ToString("yyyy-MM-dd HH:mm:ss")</td>
                                                    </tr>
                                                }
                                            }
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@section javascript{
    <script type="text/javascript">
        $(function () {
            $("#frmUpdate").validate({
                rules: {
                    ChargeMoney: { required: true, min: 1, digits: true }
                },
                messages: {
                    ChargeMoney: { required: "请填写本次充值金额" }
                }
            });

            $("#btnSave").click(function () {
                if ($("#frmUpdate").valid()) {
                    alertify.okBtn("确定").cancelBtn("取消").confirm("本次充值金额为“" + changeMoneyToChinese($("#ChargeMoney").val()) + "”，确定充值吗？", function () {
                        $.ajax({
                            type: 'post',
                            url: '@Url.Action("Charge")',
                            data: $("#frmUpdate").serialize(),
                            success: function (data) {
                                if (data.Status) {
                                    alertify.success("充值成功");
                                    setTimeout(function () {
                                        window.location.href = "/Shops/List";
                                    }, 500);
                                } else {
                                    alertify.error("充值失败，请重试！");
                                }
                            },
                            error: function () {
                                alertify.error("充值失败，请重试！");
                            }
                        });
                    });
                }
            });
        });

        function changeMoneyToChinese(money) {
            var cnNums = new Array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"); //汉字的数字
            var cnIntRadice = new Array("", "拾", "佰", "仟"); //基本单位
            var cnIntUnits = new Array("", "万", "亿", "兆"); //对应整数部分扩展单位
            var cnDecUnits = new Array("角", "分", "毫", "厘"); //对应小数部分单位
            var cnInteger = "整"; //整数金额时后面跟的字符
            var cnIntLast = "元"; //整型完以后的单位
            var maxNum = 999999999999999.9999; //最大处理的数字

            var IntegerNum; //金额整数部分
            var DecimalNum; //金额小数部分
            var ChineseStr = ""; //输出的中文金额字符串
            var parts; //分离金额后用的数组，预定义

            if (money == "") { return ""; }

            money = parseFloat(money);
            if (money >= maxNum) {
                $.alert('超出最大处理数字');
                return "";
            }
            if (money == 0) {
                ChineseStr = cnNums[0] + cnIntLast + cnInteger;
                return ChineseStr;
            }
            money = money.toString(); //转换为字符串
            if (money.indexOf(".") == -1) {
                IntegerNum = money;
                DecimalNum = '';
            } else {
                parts = money.split(".");
                IntegerNum = parts[0];
                DecimalNum = parts[1].substr(0, 4);
            }
            if (parseInt(IntegerNum, 10) > 0) {//获取整型部分转换
                zeroCount = 0;
                IntLen = IntegerNum.length;
                for (i = 0; i < IntLen; i++) {
                    n = IntegerNum.substr(i, 1);
                    p = IntLen - i - 1;
                    q = p / 4;
                    m = p % 4;
                    if (n == "0") {
                        zeroCount++;
                    } else {
                        if (zeroCount > 0) {
                            ChineseStr += cnNums[0];
                        }
                        zeroCount = 0; //归零
                        ChineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
                    }
                    if (m == 0 && zeroCount < 4) {
                        ChineseStr += cnIntUnits[q];
                    }
                }
                ChineseStr += cnIntLast;
                //整型部分处理完毕
            }
            if (DecimalNum != '') {//小数部分
                decLen = DecimalNum.length;
                for (i = 0; i < decLen; i++) {
                    n = DecimalNum.substr(i, 1);
                    if (n != '0') {
                        ChineseStr += cnNums[Number(n)] + cnDecUnits[i];
                    }
                }
            }
            if (ChineseStr == '') {
                ChineseStr += cnNums[0] + cnIntLast + cnInteger;
            }
            else if (DecimalNum == '') {
                ChineseStr += cnInteger;
            }
            return ChineseStr;
        }
    </script>
}